package com.kawa.kemanage.controller.app;

import com.kawa.kemanage.service.DoLikeService;
import com.kawa.kemanage.util.Result;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping({"/app/like"})
public class DoLikeColltroller {
    @Autowired
    private DoLikeService doLikeService;

    /**
     * 查询所有新闻的点赞数
     *
     * @return
     */
    @RequestMapping("/allCount")
    public Result queryAllNewsLikesCount() {
        try {
            return Result.success(doLikeService.queryAllLikesCount());
        } catch (Exception e) {
            return Result.error("获取点赞失败："+e.getMessage());
        }

    }

    /**
     * 查询是否点赞
     * @param userId 用户id
     * @return
     */
    @GetMapping("/userLikes")
    public Result getUserLikedContentIds(@RequestParam int userId) {
        try {
            // 这里假设你的Service有对应方法
            List<Integer> likedIds = doLikeService.getUserLikedContentIds(userId);
            return Result.success(likedIds);
        } catch (Exception e) {
            return Result.error("获取用户点赞失败：" + e.getMessage());
        }
    }

    /**
     * 根据新闻id查询对应的点赞总数
     *
     * @param newsId 新闻id
     * @return
     */
    @RequestMapping("/newsLikesCount/{newsId}")
    public Result queryNewsLikesCount(@PathVariable int newsId) {
        try {
            return Result.success(doLikeService.queryLikesCountByNewsId(newsId));
        } catch (Exception e) {
            return Result.error("获取点赞失败："+e.getMessage());
        }

    }

    /**
     * 批量点赞和取消点赞
     * @param request
     * @return
     */
    @PostMapping("/batch")
    public Result handleBatchOperations(@RequestBody BatchLikeRequest request) {
        try {
            for (LikeOperation op : request.getOperations()) {
                if ("add".equals(op.getType())) {
                    doLikeService.addLike(op.getUserId(), op.getContentId());
                } else if ("cancel".equals(op.getType())) {
                    doLikeService.cancelLike(op.getUserId(), op.getContentId());
                }
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error("批量操作失败: " + e.getMessage());
        }
    }

    // 新增请求体类
    @Data
    static class BatchLikeRequest {
        private List<LikeOperation> operations;
    }

    @Data
    static class LikeOperation {
        private String type; // "add" 或 "cancel"
        private int userId;
        private int contentId;
    }

    /**
     * 增加点赞（只支持单个）
     *
     * @param userId 用户id
     * @param contentId 内容id
     * @return
     */
    @RequestMapping ("/add/{userId}/{contentId}")
    public Result addLike(@PathVariable int userId, @PathVariable int contentId) {
        boolean success = doLikeService.addLike(userId, contentId);
        return success ? Result.success() : Result.error("点赞失败");
    }

    /**
     * 取消点赞（只支持单个）
     *
     * @param userId 用户id
     * @param contentId 内容id
     * @return
     */
    @RequestMapping ("/cancel/{userId}/{contentId}")
    public Result cancelLike(@PathVariable int userId, @PathVariable int contentId) {
        boolean success = doLikeService.cancelLike(userId, contentId);
        return success ? Result.success() : Result.error("取消失败");
    }

    /**
     * 点赞管理接口
     */
    @RequestMapping("/likesByUserId")
    public Result likesByUserId(@RequestParam int userId) {
        try {
            return Result.success(doLikeService.getLikesForUserArticles(userId));
        }
        catch (Exception e) {
            return Result.error("获取用户点赞失败：" + e.getMessage());
        }
    }

}
